package com.example.bumenguanli.Service;

import com.example.bumenguanli.Mapper.WorkerMapper;
import com.example.bumenguanli.Unity.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class WorkerServiceImpl implements WorkerService {

    @Autowired
    private WorkerMapper workerMapper;

    @Override
    public Worker create(Worker worker) {
        worker.setDate(LocalDate.now());
        workerMapper.insert(worker);
        return worker;
    }

    @Override
    public void delete(Integer id) {
        workerMapper.delete(id);
    }

    @Override
    public Worker update(Integer id, Worker worker) {
        worker.setId(id);
        worker.setDate(LocalDate.now());
        workerMapper.update(worker);
        return workerMapper.select(id); // 返回更新后的数据
    }

    @Override
    public Worker select(Integer id) {
        // 修正：直接返回查询结果，而不是未定义的变量
        return workerMapper.select(id);
    }

    @Override
    public List<Worker> selectAll() {
        return workerMapper.selectAll();
    }
}